package com.liang.manage.auth.facade.server.iml;

import com.liang.manage.auth.facade.dto.role.RolePermissionsDTO;
import com.liang.manage.auth.facade.dto.role.RolePermissionsInDTO;
import com.liang.manage.auth.facade.entity.RolePermissionsPo;
import com.liang.manage.auth.facade.entity.RolePermissionsPoExample;
import com.liang.manage.auth.facade.mapper.RolePermissionsPoMapper;
import com.liang.manage.auth.facade.mapstruct.RolePermissionsMS;
import com.liang.manage.auth.facade.server.RolePermissionsService;

import com.liang.nansheng.common.auth.UserSsoDTO;
import com.liang.nansheng.common.enums.ResponseCode;
import com.liang.nansheng.common.web.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 王华安
 * @date 2021-04-25 23:26
 */
@Slf4j
@Service
@Component
public class RolePermissionsServiceImpl implements RolePermissionsService {
    @Autowired
    private RolePermissionsPoMapper rolePermissionsPoMapper;

    /**
     * 根据角色id获取角色权限信息
     *
     * @param roleId
     * @return
     */
    @Override
    public List<RolePermissionsDTO> getByRoleId(Integer roleId) {
        RolePermissionsPoExample example = new RolePermissionsPoExample();
        example.createCriteria().andIsDeletedEqualTo(false)
                .andRoleIdEqualTo(roleId);

        return RolePermissionsMS.INSTANCE.toDTO(rolePermissionsPoMapper.selectByExample(example));
    }

    /**
     * 根据权限id获取角色权限信息
     *
     * @param permissionsIdId
     * @return
     */
    @Override
    public List<RolePermissionsDTO> getByPermissionsId(Integer permissionsIdId) {
        RolePermissionsPoExample example = new RolePermissionsPoExample();
        example.createCriteria().andIsDeletedEqualTo(false)
                .andPermissionsIdEqualTo(permissionsIdId);

        return RolePermissionsMS.INSTANCE.toDTO(rolePermissionsPoMapper.selectByExample(example));
    }

    /**
     * 根据角色id删除角色权限
     *
     * @param roleId
     * @param currentUser
     * @return
     */
    @Override
    public Boolean deleteByRoleId(Integer roleId, UserSsoDTO currentUser) {
        RolePermissionsPoExample example = new RolePermissionsPoExample();
        example.createCriteria().andIsDeletedEqualTo(false)
                .andRoleIdEqualTo(roleId);

        RolePermissionsPo rolePermissionsPo = new RolePermissionsPo();
        rolePermissionsPo.setIsDeleted(true);
        rolePermissionsPo.setRoleId(roleId);
        rolePermissionsPo.setUpdateUser(currentUser.getUserId());
        rolePermissionsPo.setUpdateTime(LocalDateTime.now());

        if (rolePermissionsPoMapper.updateByExampleSelective(rolePermissionsPo, example) <= 0) {
            throw BusinessException.build(ResponseCode.OPERATE_FAIL, "删除角色权限失败");
        }

        return true;
    }

    /**
     * 根据权限id删除角色权限
     *
     * @param permissionsId
     * @param currentUser
     * @return
     */
    @Override
    public Boolean deleteByPermissionsId(Integer permissionsId, UserSsoDTO currentUser) {
        RolePermissionsPoExample example = new RolePermissionsPoExample();
        example.createCriteria().andIsDeletedEqualTo(false)
                .andPermissionsIdEqualTo(permissionsId);

        RolePermissionsPo rolePermissionsPo = new RolePermissionsPo();
        rolePermissionsPo.setIsDeleted(true);
        rolePermissionsPo.setPermissionsId(permissionsId);
        rolePermissionsPo.setUpdateUser(currentUser.getUserId());
        rolePermissionsPo.setUpdateTime(LocalDateTime.now());

        if (rolePermissionsPoMapper.updateByExampleSelective(rolePermissionsPo, example) <= 0) {
            throw BusinessException.build(ResponseCode.OPERATE_FAIL, "删除角色权限失败");
        }

        return true;
    }

    /**
     * 添加权限
     *
     * @param rolePermissionsInDTO
     * @param currentUser
     * @return
     */
    @Override
    public Boolean addPermissions(RolePermissionsInDTO rolePermissionsInDTO, UserSsoDTO currentUser) {
        RolePermissionsPo rolePermissionsPo = new RolePermissionsPo();
        rolePermissionsPo.setIsDeleted(false);
        rolePermissionsPo.setRoleId(rolePermissionsInDTO.getRoleId());
        rolePermissionsPo.setCreateUser(currentUser.getUserId());
        rolePermissionsPo.setUpdateUser(currentUser.getUserId());
        LocalDateTime now = LocalDateTime.now();
        rolePermissionsPo.setCreateTime(now);
        rolePermissionsPo.setUpdateTime(now);

        rolePermissionsInDTO.getPermissionsIds().forEach(permissionsId -> {
            rolePermissionsPo.setPermissionsId(permissionsId);
            if (rolePermissionsPoMapper.insertSelective(rolePermissionsPo) <= 0) {
                throw BusinessException.build(ResponseCode.OPERATE_FAIL, "添加权限失败");
            }
        });

        return true;
    }

    /**
     * 移除权限
     *
     * @param rolePermissionsInDTO
     * @param currentUser
     * @return
     */
    @Override
    public Boolean removePermissions(RolePermissionsInDTO rolePermissionsInDTO, UserSsoDTO currentUser) {
        List<RolePermissionsDTO> rolePermissionsDTOS = getByRoleId(rolePermissionsInDTO.getRoleId());
        Map<Integer, Integer> map = rolePermissionsDTOS.stream()
                .collect(Collectors.toMap(RolePermissionsDTO::getPermissionsId, RolePermissionsDTO::getId, (v1, v2) -> v1));
        rolePermissionsInDTO.getPermissionsIds().forEach(permissionsId -> {
            // 需要移除的权限
            if (map.containsKey(permissionsId)) {
                RolePermissionsDTO rolePermissionsDTO = new RolePermissionsDTO();
                rolePermissionsDTO.setId(map.get(permissionsId));
                rolePermissionsDTO.setIsDeleted(true);
                rolePermissionsDTO.setUpdateUser(currentUser.getUserId());
                rolePermissionsDTO.setUpdateTime(LocalDateTime.now());
                if (rolePermissionsPoMapper.updateByPrimaryKeySelective(RolePermissionsMS.INSTANCE.toPo(rolePermissionsDTO)) <= 0) {
                    throw BusinessException.build(ResponseCode.OPERATE_FAIL, "移除权限失败");
                }
            }
        });

        return true;
    }
}
